//
//  HOTMessageHandler.m
//  Genpara
//
//  Created by AndyChang.zhang on 14/11/10.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTMessageHandler.h"
#import "ZZZPopupView.h"
#import "HOTMessageBoxViewController.h"
#import "HOTAppDelegate.h"
#import "HOTNaviBar.h"
#import "HOTNavigationViewController.h"
#import "HOTMessageChatViewController.h"

static HOTMessageHandler *obj;

@implementation HOTMessageHandler


+(instancetype)handler {
    
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        obj = [[HOTMessageHandler alloc] init];
    });
    
    return obj;
}

-(void)handleMessageNotification:(NSDictionary *)userInfo {

    
    NSString *type = [userInfo valueForKey:@"type"];
    NSDictionary *aps = [userInfo valueForKey:@"aps"];
    NSString *content = [aps objectForKey:@"alert"];
    
    if ([HOTUtils isNotNullOrEmpty:type]) {
        
        if ([type isEqualToString:@"article_repied"]) {
            //自己的投稿被回复时
            
            //NSString *content = [userInfo objectForKey:@"title"];
            ZZZPopupView *alertView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE message:content delegate:nil cancelButtonTitle:nil otherButtonTitle:ALERT_BTN_OK];
            
            [alertView showInWindow];
            
        } else if ([type isEqualToString:@"was_followed"]) {
            //被别人设置为 “知り合い’”
            
            //NSString *content = [userInfo objectForKey:@"title"];
            ZZZPopupView *alertView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE message:content delegate:nil cancelButtonTitle:nil otherButtonTitle:ALERT_BTN_OK];
            
            [alertView showInWindow];
            
        } else if ([type isEqualToString:@"new_msg_incoming"]) {
            //Message聊天里来新消息时(通知)
            NSString *user_id = [userInfo objectForKey:@"user_id"];
            HOTAppDelegate * delegate = (HOTAppDelegate *)[[UIApplication sharedApplication] delegate];
            
            UIViewController *presentVc = delegate.navc.topViewController.presentedViewController;
            if (presentVc) {
                
                if ([presentVc isKindOfClass:[UINavigationController class]]) {
                    
                    presentVc = ((UINavigationController *)presentVc).topViewController;
                }
            }
            
            UIViewController *newVc = presentVc ? presentVc : delegate.navc.topViewController;
            
            if ([newVc.navigationController.topViewController isKindOfClass:[HOTMessageChatViewController class]]
                && [((HOTMessageChatViewController *)newVc.navigationController.topViewController).target_id isEqualToString:user_id]) {
                //如果当前已经在聊天界面，并且聊天的对象是一个的话，画面不做迁移
                return;
            }
            
            //NSString *content = [userInfo objectForKey:@"title"];
            ZZZPopupView *alertView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE message:content delegate:self cancelButtonTitle:ALERT_BTN_CANCEL otherButtonTitle:ALERT_BTN_OK];
            
            alertView.tag = 1501;
            
            [alertView showInWindow];
            
        } else if ([type isEqualToString:@"system_msg_incoming"]) {
            //后台运营方发来的系统通知 “お知らせ” 时

            //NSString *content = [userInfo objectForKey:@"title"];
            ZZZPopupView *alertView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE message:content delegate:self cancelButtonTitle:ALERT_BTN_CANCEL otherButtonTitle:ALERT_BTN_OK];
            
            alertView.tag = 1502;
            
            [alertView showInWindow];
        }
    }
}

-(void)popupView:(ZZZPopupView *)popupView buttonKind:(ZZZPopupViewBtnKind)btnKind {
    
    if (popupView.tag == 1501 && btnKind == ZZZPopupViewConfirmBtnKind) {
        //Message聊天里来新消息时(通知)
        [self showMessageBoxViewController];
    } else if (popupView.tag == 1502 && btnKind == ZZZPopupViewConfirmBtnKind) {
        //后台运营方发来的系统通知 “お知らせ” 时
        [self showMessageBoxViewController];
    }
}

-(void)showMessageBoxViewController {
    
    HOTAppDelegate * delegate = (HOTAppDelegate *)[[UIApplication sharedApplication] delegate];

    UIViewController *presentVc = delegate.navc.topViewController.presentedViewController;
    if (presentVc) {
        
        if ([presentVc isKindOfClass:[UINavigationController class]]) {
            
            presentVc = ((UINavigationController *)presentVc).topViewController;
        }
    }
    
    UIViewController *newVc = presentVc ? presentVc : delegate.navc.topViewController;
    
    if ([newVc.navigationController.topViewController isKindOfClass:[HOTMessageBoxViewController class]]) {
        //当前已经在message box列表界面
        return;
    }
    
    if ([newVc isKindOfClass:[HOTMessageChatViewController class]]) {
        //当前在聊天界面时
        if (newVc.navigationController.viewControllers.count > 1) {
            
            UIViewController *vc = [newVc.navigationController.viewControllers objectAtIndex:(newVc.navigationController.viewControllers.count -2)];
            if ([vc isKindOfClass:[HOTMessageBoxViewController class]]) {
                
                //pop出到上一级（messagebox 列表界面）
                [newVc.navigationController popViewControllerAnimated:YES];
            } else {
                //弹出messagebox 列表
                [self presentMsgBoxViewController:newVc];
            }
        } else {
            //弹出messagebox 列表
            [self presentMsgBoxViewController:newVc];
        }
    } else {
        
        //弹出messagebox 列表
        [self presentMsgBoxViewController: newVc];
    }
}

-(void)presentMsgBoxViewController:(UIViewController *)vc {
    
    HOTMessageBoxViewController *mbvc = [[HOTMessageBoxViewController alloc] initWithNibName:@"HOTMessageBoxViewController" bundle:nil];
    
    HOTNavigationViewController *hotnavc = [[HOTNavigationViewController alloc] initWithNavigationBarClass:[HOTNaviBar class] toolbarClass:nil];
    
    [hotnavc pushViewController:mbvc animated:NO];
    
    //HOTAppDelegate * delegate = (HOTAppDelegate *)[[UIApplication sharedApplication] delegate];
    [vc presentViewController:hotnavc animated:YES completion:nil];
}
@end
